/*
 * @author Steve Cox
 * @date 10/20/2011
 * @description Unit tests for MM_EquipmentSummaryController
 */
@isTest
private class MM_EquipmentSummaryControllerTests {
   private static List<Equipment__c> equipment = new List<Equipment__c>();
   private static List<Task> todos = new List<Task>();
   
   static private void initData() {
      // create supporting objects
      Account implantCenter = MM_Test.newImplantCenter();
      insert implantCenter;

      Patient__c patient = MM_Test.newPatient(implantCenter.Id);
      insert patient;

      equipment = new List<Equipment__c> {
         MM_Test.newEquipment(1), MM_Test.newEquipment(2), MM_Test.newEquipment(3), 
         MM_Test.newEquipment(4), MM_Test.newEquipment(5), MM_Test.newEquipment(6), 
         MM_Test.newEquipment(7)};
      insert equipment;

      todos = new List<Task> {
         MM_Test.newTodo(patient), MM_Test.newTodo(patient)};
      insert todos;
   }

   static testMethod void testEquipment() {
      initData();

      Test.startTest();
      MM_EquipmentSummaryController controller = new MM_EquipmentSummaryController();
      System.assert(controller.equipment.size() >= equipment.size());
      
      // pagination
      controller.equipPageSize = 3;
      controller.go();
      System.assertEquals(3, controller.equipPageSize);
      System.assertEquals(3, controller.equipment.size());
      System.assertEquals(1, controller.equipPage);
      System.assert(controller.equipCount >= equipment.size());
      System.assert(controller.equipHasNext);
      System.assert(!controller.equipHasPrev);
      System.assert(controller.equipPageOf.length() > 0);

      controller.equipNext();
      System.assertEquals(2, controller.equipPage);
      System.assertEquals(3, controller.equipment.size());
      System.assert(controller.equipHasNext);
      System.assert(controller.equipHasPrev);

      controller.equipPrev();
      System.assertEquals(1, controller.equipPage);
      System.assertEquals(3, controller.equipment.size());
      System.assert(controller.equipHasNext);
      System.assert(!controller.equipHasPrev);

      controller.equipLast();
      System.assert(controller.equipPage > 2);
      System.assert(!controller.equipHasNext);
      System.assert(controller.equipHasPrev);

      controller.equipFirst();
      System.assertEquals(1, controller.equipPage);
      System.assertEquals(3, controller.equipment.size());
      System.assert(controller.equipHasNext);
      System.assert(!controller.equipHasPrev);

      // checked equipment
      controller.allChecked = true;
      controller.checkAll();
      System.assert(controller.equipment[0].isSelected);
      controller.allChecked = false;
      controller.checkAll();
      System.assert(!controller.equipment[0].isSelected);
      
      //TODO show filter
      controller.show = 'junk';
      System.assert(controller.showOptions.size() >= 0);
      //TODO limit to filter
      controller.limitTo = 'junk';
      System.assert(controller.limitToOptions.size() >= 0);
      //TODO SN/LOT filter
      controller.snLot = 'junk';
      //TODO openServiceRequestsOnly
      controller.openServiceRequestsOnly = true;
      
      //TODO sorting
      controller.sortField = 'Name';
      controller.sortEquipment();
      for (Integer i = 0; i < controller.equipment.size(); ++i) {
         if (i < controller.equipment.size() - 1) {
            System.assert(controller.equipment[i].equipment.Name <= 
               controller.equipment[i+1].equipment.Name);
         }
      }

      // descending sort
      controller.sortEquipment();
      for (Integer i = 0; i < controller.equipment.size(); ++i) {
         if (i < controller.equipment.size() - 1) {
            System.assert(controller.equipment[i].equipment.Name >= 
               controller.equipment[i+1].equipment.Name);
         }
      }
      Test.stopTest();
   }

   static testMethod void testToDos() {
      initData();
      
      Test.startTest();
      MM_EquipmentSummaryController controller = new MM_EquipmentSummaryController();
      
      PageReference pageRef = Page.MM_EquipmentSummary;
      pageRef.getParameters().put('id', todos[0].Id);
      Test.setCurrentPage(pageRef);
      controller.todoSetContext();
      System.assert(controller.todo.Id == todos[0].Id);

      //TODO save
      controller.saveToDo();
      Test.stopTest();
   }

   static testMethod void testInventory() {
      initData();
      //TODO
   }
}